#include "model.h"
#include "exercise.h"
#include "ui_exercise.h"
#include <QDebug>
#include <QTextBrowser>
#include <QLabel>
#include <QRadioButton>
#include "global.h"
#include <random>
#include <string>
#include <QDebug>
#include <random>
#include <QPushButton>

int getNum()
{
    int a;
    a=rand();
    a=a%4;
    return a;
}

int random=0;
double answer=0;
double WA1,WA2,WA3;
std::string question;


Exercise::Exercise(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Exercise)
{
    ui->setupUi(this);
    extern int grade;
    //ui->textBrowser->
    setWindowTitle(QString("小学生速算练习软件"));

    //生成题目
    random = getNum();
    question = "";
    getQA(grade,&question,&answer,&WA1,&WA2,&WA3);
    QString Qquestion = QString::fromStdString(question);
    //qDebug()<<Qquestion
    ui->makeQ->setText(QString("问题："+ Qquestion));
    ui->NextBtn->setDisabled(true);

    if(random==0)
    {
        ui->answerA->setText(QString("%1").arg(answer));
        ui->answerB->setText(QString("%1").arg(WA1));
        ui->answerC->setText(QString("%1").arg(WA2));
        ui->answerD->setText(QString("%1").arg(WA3));
    }

    else if(random==1)
    {
        ui->answerA->setText(QString("%1").arg(WA1));
        ui->answerB->setText(QString("%1").arg(answer));
        ui->answerC->setText(QString("%1").arg(WA2));
        ui->answerD->setText(QString("%1").arg(WA3));
    }

    else if(random==2)
    {
        ui->answerA->setText(QString("%1").arg(WA2));
        ui->answerB->setText(QString("%1").arg(WA1));
        ui->answerC->setText(QString("%1").arg(answer));
        ui->answerD->setText(QString("%1").arg(WA3));
    }


    else if(random==3)
    {
        ui->answerA->setText(QString("%1").arg(WA3));
        ui->answerB->setText(QString("%1").arg(WA1));
        ui->answerC->setText(QString("%1").arg(WA2));
        ui->answerD->setText(QString("%1").arg(answer));
    }


    connect(ui->SumbitBtn,&QPushButton::clicked,this,[=](){

        if(random==0)
        {
            ui->answerA->setText(QString("%1").arg(answer));
            ui->answerB->setText(QString("%1").arg(WA1));
            ui->answerC->setText(QString("%1").arg(WA2));
            ui->answerD->setText(QString("%1").arg(WA3));
            //确认答案是否正确
            if(ui->answerA->isChecked()==true)
            {
                ui->textBrowser->insertPlainText(QString("答案正确\n"));
                ui->NextBtn->setEnabled(true);
                ui->SumbitBtn->setDisabled(true);
                disconnect(ui->SumbitBtn);
                //qDebug()<<"1AT";
            }
            if(ui->answerA->isChecked()==false)
            {
                ui->textBrowser->insertPlainText(QString("答案错误，正确答案为A\n"));
                ui->SumbitBtn->setDisabled(true);
                ui->NextBtn->setEnabled(true);
                disconnect(ui->SumbitBtn);
                //qDebug()<<"1AW";
            }
        }

        else if(random==1)
        {
            ui->answerA->setText(QString("%1").arg(WA1));
            ui->answerB->setText(QString("%1").arg(answer));
            ui->answerC->setText(QString("%1").arg(WA2));
            ui->answerD->setText(QString("%1").arg(WA3));
            if(ui->answerB->isChecked()==true)
            {
                ui->textBrowser->insertPlainText(QString("答案正确\n"));
                ui->NextBtn->setEnabled(true);
                ui->SumbitBtn->setDisabled(true);
                disconnect(ui->SumbitBtn);
                //qDebug()<<"1BT";
            }
            if(ui->answerB->isChecked()==false)
            {
                ui->textBrowser->insertPlainText(QString("答案错误，正确答案为B\n"));
                ui->SumbitBtn->setDisabled(true);
                ui->NextBtn->setEnabled(true);
                disconnect(ui->SumbitBtn);
                //qDebug()<<"1BW";

            }
        }

        else if(random==2)
        {
            ui->answerA->setText(QString("%1").arg(WA2));
            ui->answerB->setText(QString("%1").arg(WA1));
            ui->answerC->setText(QString("%1").arg(answer));
            ui->answerD->setText(QString("%1").arg(WA3));

            if(ui->answerC->isChecked()==true)
            {
                ui->textBrowser->insertPlainText(QString("答案正确\n"));
                ui->NextBtn->setEnabled(true);
                ui->SumbitBtn->setDisabled(true);
                disconnect(ui->SumbitBtn);
                //qDebug()<<"1CT";
            }
            if(ui->answerC->isChecked()==false)
            {
                ui->textBrowser->insertPlainText(QString("答案错误，正确答案为C\n"));
                ui->SumbitBtn->setDisabled(true);
                ui->NextBtn->setEnabled(true);
                disconnect(ui->SumbitBtn);
                //qDebug()<<"1CW";
            }
        }


        else if(random==3)
        {
            ui->answerA->setText(QString("%1").arg(WA3));
            ui->answerB->setText(QString("%1").arg(WA1));
            ui->answerC->setText(QString("%1").arg(WA2));
            ui->answerD->setText(QString("%1").arg(answer));

            if(ui->answerD->isChecked()==true)
            {
                ui->textBrowser->insertPlainText(QString("答案正确\n"));
                ui->SumbitBtn->setDisabled(true);
                ui->NextBtn->setEnabled(true);
                disconnect(ui->SumbitBtn);
                //qDebug()<<"1DT";
            }
            if(ui->answerD->isChecked()==false)
            {
                ui->textBrowser->insertPlainText(QString("答案错误，正确答案为D\n"));
                ui->SumbitBtn->setDisabled(true);
                ui->NextBtn->setEnabled(true);
                disconnect(ui->SumbitBtn);
                //qDebug()<<"1DW";
            }
        }
    });



    //下一题生成：
    connect(ui->NextBtn,&QPushButton::clicked,this,[&](){


        ui->SumbitBtn->setEnabled(true);
        ui->NextBtn->setDisabled(true);
        //生成题目：
        random=getNum();
        //生成题目
        question = "";
        getQA(grade,&question,&answer,&WA1,&WA2,&WA3);
        QString Qquestion = QString::fromStdString(question);
        //qDebug()<<Qquestion
        ui->makeQ->setText(QString("问题："+ Qquestion));
        ui->NextBtn->setDisabled(true);
        if(random==0)
        {
            ui->answerA->setText(QString("%1").arg(answer));
            ui->answerB->setText(QString("%1").arg(WA1));
            ui->answerC->setText(QString("%1").arg(WA2));
            ui->answerD->setText(QString("%1").arg(WA3));
        }

        else if(random==1)
        {
            ui->answerA->setText(QString("%1").arg(WA1));
            ui->answerB->setText(QString("%1").arg(answer));
            ui->answerC->setText(QString("%1").arg(WA2));
            ui->answerD->setText(QString("%1").arg(WA3));
        }

        else if(random==2)
        {
            ui->answerA->setText(QString("%1").arg(WA2));
            ui->answerB->setText(QString("%1").arg(WA1));
            ui->answerC->setText(QString("%1").arg(answer));
            ui->answerD->setText(QString("%1").arg(WA3));
        }


        else if(random==3)
        {
            ui->answerA->setText(QString("%1").arg(WA3));
            ui->answerB->setText(QString("%1").arg(WA1));
            ui->answerC->setText(QString("%1").arg(WA2));
            ui->answerD->setText(QString("%1").arg(answer));
        }


    });
}

Exercise::~Exercise()
{
    delete ui;
}
